﻿
Imports osi.root.constants
Imports osi.root.connector
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils
Imports osi.service.argument

Public MustInherit Class idevice_pool_adapter(Of FROM_DEV, TO_DEV, T As idevice_pool(Of FROM_DEV))
    Inherits idevice_pool_wrapper(Of T)
    Implements idevice_pool(Of TO_DEV)

    Public Sub New(ByVal f As T)
        MyBase.New(f)
    End Sub

    Protected MustOverride Function to_dev_to_from_dev(ByVal i As TO_DEV, ByRef o As FROM_DEV) As Boolean

    Public Function release(ByVal c As TO_DEV) As Boolean Implements idevice_pool(Of TO_DEV).release
        Dim o As FROM_DEV = Nothing
        Return to_dev_to_from_dev(c, o) AndAlso
               impl.release(o)
    End Function
End Class

Public MustInherit Class idevice_pool_adapter(Of FROM_DEV, TO_DEV)
    Inherits idevice_pool_adapter(Of FROM_DEV, TO_DEV, idevice_pool(Of FROM_DEV))

    Public Sub New(ByVal f As idevice_pool(Of FROM_DEV))
        MyBase.New(f)
    End Sub
End Class

Public MustInherit Class isync_device_pool_adapter(Of FROM_DEV, TO_DEV, T As isync_device_pool(Of FROM_DEV))
    Inherits idevice_pool_adapter(Of FROM_DEV, TO_DEV, T)
    Implements isync_device_pool(Of TO_DEV)

    Public Sub New(ByVal f As T)
        MyBase.New(f)
    End Sub

    Protected MustOverride Function from_dev_to_to_dev(ByVal i As FROM_DEV, ByRef o As TO_DEV) As Boolean

    Public Function sync_get(ByRef o As TO_DEV) As Boolean Implements isync_device_pool(Of TO_DEV).sync_get
        Dim c As FROM_DEV = Nothing
        If impl.sync_get(c) Then
            Return from_dev_to_to_dev(c, o)
        Else
            Return False
        End If
    End Function
End Class

Public MustInherit Class isync_device_pool_adapter(Of FROM_DEV, TO_DEV)
    Inherits isync_device_pool_adapter(Of FROM_DEV, TO_DEV, isync_device_pool(Of FROM_DEV))

    Public Sub New(ByVal f As isync_device_pool(Of FROM_DEV))
        MyBase.New(f)
    End Sub
End Class

Public MustInherit Class iasync_device_pool_adapter(Of FROM_DEV, TO_DEV, T As iasync_device_pool(Of FROM_DEV))
    Inherits idevice_pool_adapter(Of FROM_DEV, TO_DEV, T)
    Implements iasync_device_pool(Of TO_DEV)

    Public Sub New(ByVal f As T)
        MyBase.New(f)
    End Sub

    Protected MustOverride Function from_dev_to_to_dev(ByVal i As FROM_DEV, ByVal o As pointer(Of TO_DEV)) As event_comb

    Public Function [get](ByVal o As pointer(Of TO_DEV)) As event_comb Implements iasync_device_pool(Of TO_DEV).get
        Dim ec As event_comb = Nothing
        Dim p As pointer(Of FROM_DEV) = Nothing
        Return New event_comb(Function() As Boolean
                                  p = New pointer(Of FROM_DEV)()
                                  ec = impl.get(p)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  If ec.end_result() AndAlso
                                     assert(Not +p Is Nothing) Then
                                      ec = from_dev_to_to_dev(+p, o)
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  Else
                                      Return False
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function
End Class

Public MustInherit Class iasync_device_pool_adapter(Of FROM_DEV, TO_DEV)
    Inherits iasync_device_pool_adapter(Of FROM_DEV, TO_DEV, iasync_device_pool(Of FROM_DEV))

    Public Sub New(ByVal f As iasync_device_pool(Of FROM_DEV))
        MyBase.New(f)
    End Sub
End Class

Public MustInherit Class new_device_event_isync_device_pool_adapter(Of _
                             FROM_DEV,
                             TO_DEV,
                             T As new_device_event_isync_device_pool(Of FROM_DEV))
    Inherits isync_device_pool_adapter(Of FROM_DEV, TO_DEV, T)
    Implements new_device_event_isync_device_pool(Of TO_DEV)

    Public Event new_device_inserted() Implements new_device_event_device_pool.new_device_inserted

    Public Sub New(ByVal f As T)
        MyBase.New(f)
        AddHandler f.new_device_inserted,
                   Sub()
                       RaiseEvent new_device_inserted()
                   End Sub
    End Sub
End Class

Public MustInherit Class new_device_event_isync_device_pool_adapter(Of FROM_DEV, TO_DEV)
    Inherits new_device_event_isync_device_pool_adapter(Of FROM_DEV, 
                                                           TO_DEV, 
                                                           new_device_event_isync_device_pool(Of FROM_DEV))

    Public Sub New(ByVal f As new_device_event_isync_device_pool(Of FROM_DEV))
        MyBase.New(f)
    End Sub
End Class
